// Copyright 2009, 2010 Sander Dijkhuis <sander.dijkhuis@gmail.com>
//
// This file is part of Pleft.
//
// Pleft is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pleft is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pleft. If not, see <http://www.gnu.org/licenses/>.

/**
 * @fileoverview The Pleft appointment overview.
 * @author alcinnz@gmail.com (Adrian Cochrane)
 */

// TODO(sander) doesn't pass jslint yet. (documentation not needed for simpler code, FIXED)

//This is a rewrite to work with the Graceful Degradation rewrite of template and views.
//It'll make use of jQuery and some JavaScriptMVC to be concise.
//May fix above TODO
cons MIDDLE_OFFSET;
var data;
var _ = getText;

function showAvailability(avail) {
  function createAvailability(avail) {
    return $('<li>').class('balloon').append(
        $('<span>').class(avail.possible).text(avail.possible)
      ).append(avail.comment);
  }
  
  var el = $('#availability').empty();
  for (var i=0; i<avail.length; i++) $(el).append(createAvail(avail[i]));
}

function createAvailabilityForm(avail) {
  function createAvailability(avail, i) {
    var rep = '<td><input type="radio" name="'+i+'-avail" value="no" />'+_("No")+'</td><td><input type="radio" name="'+i+'-avail" value="maybe" />'+_("Maybe")+'</td>' +
            '<td><input type="radio" name="'+i+'-avail" value="yes" />'+_("Yes")+'</td><td><textarea name="'+i+'-comment">'+avail.comment+'</textarea></td>';
            
    var el =  $('<tr>').append(rep).class(avail.possible);
    $(el, ':value='+avail.possible).attr('checked', "checked");
    return el;
  }
  
  $('#availability').empty().append(
    "<table><tr><th>"+_("No")+"</th><th>"+_("Maybe")+"</th><th>"+_("Yes")+"</th><th>"+_("Comments?")+"</th></tr></table>");
  var el = $('#availablity table');
  
  for (var i=0; i<avail.length; i++) $(el).append(createAvail(avail[i], i)); //TODO: Check appropriate checkbox.
}

$(document).ready(function() {
//Add Ajax handler to all forms.
$('form').click(function(evt) {
  $.ajax($(evt.el).attr('action'), {
    data : $(evt.el).serialize(),
    type : $(evt.el).attr('method')
  });
  evt.cancel();
});

//Add refresh code TODO: Replace with Orbitted code to increase experiance.
$('#refresh-bttn').click(function(evt) {
  $.ajax('data/', {
    success : function(rdata) {
      data = rdata;
      
      //reload selected. 
      $("#selected").click();

      //We will leave added invitees and dates, as well as general information to be reloaded with page refresh. 
      //This may change when Orbitted is included, which will push data to the page. 
    }
  });
  evt.cancel();
});
$.ajax('data/', {
  success : function(rdata) {data = rdata;}
});

//Add selection code (lowers bandwidth for JS clients)
$('#dates li').click(function(evt) {
  //get availibility for date
  var avail = []; var offset = $(evt.el).index();
  for (var user in data.availability) {avail.push(data.availability[user][offset]);}
  
  showAvailability(avail);
  
  $(evt.el).id('selected');
  $('#arrow').style('top', $(evt.el).style('top') + CENTER_OFFSET).style('float', 'left');
  evt.cancel()
});
$('#invitees li').click(function(evt) {
  var avail = data.availability[$(evt.el).text()];
  
  if ($(evt.el).text() == data.user.name) showAvailability(avail);
  else createAvailabilityForm(avail);
  
  $(evt.el).id('selected');
  $('#arrow').style('top', $(evt.el).style('top') + CENTER_OFFSET).style('float', 'right');
  evt.cancel();
});

});
